package handler

import (
	"context"
	"github.com/golang/protobuf/ptypes/empty"
	"kop/libs/database"
	"kop/modules/mail/entity"
	"kop/modules/mail/models"
	"kop/pb"
	"kop/pb/service"
	"time"
)

type MailServiceHandler struct {
}

func (h *MailServiceHandler) GetMails(ctx context.Context, req *service.PageRequest, resp *pb.Mails) error {

	var timestamp = time.Now().AddDate(0, 0, -7)
	timestamp = time.Date(timestamp.Year(), timestamp.Month(), timestamp.Day(), 0, 0, 0, 0, time.Local)

	_ = models.DelBeforeDay(database.DB, req.RoleID, timestamp)

	var mails = models.GetMails(database.DB, req.RoleID, req.Page, req.PageNum)

	resp.Mails = make([]*pb.Mail, 0, len(mails))
	for _, mail := range mails {
		resp.Mails = append(resp.Mails, buildPbMail(mail))
	}

	return nil
}

func (h *MailServiceHandler) GetMail(ctx context.Context, req *service.DefaultRequest, resp *pb.Mail) error {

	var mail, err = models.GetMail(database.DB, req.RoleID, req.Value)
	if err == nil {
		*resp = *buildPbMail(mail)
	}

	return err
}

func (h *MailServiceHandler) Read(ctx context.Context, req *service.DefaultRequest, e *empty.Empty) error {

	return models.Read(database.DB, req.RoleID, req.Value)
}

func (h *MailServiceHandler) Attachment(ctx context.Context, req *service.DefaultRequest, resp *pb.Award) error {

	var mail, err = models.GetMail(database.DB, req.RoleID, req.Value)
	if err != nil {
		return err
	}

	*resp = *mail.AttachmentStruct.Award
	mail.AttachmentStruct.Received = true
	return models.Save(database.DB, &mail)
}

func (h *MailServiceHandler) Delete(ctx context.Context, req *service.DeleteMailRequest, e *empty.Empty) error {

	return models.Delete(database.DB, req.RoleID, req.IDs)
}

func (h *MailServiceHandler) Send(ctx context.Context, req *service.SendMailRequest, e *empty.Empty) error {
	//
	//var table = uuid.New().String()
	//
	//var err = database.DB.Exec("create temp table mail_" + table + "(role_id int  primary key)").Error
	//if err != nil {
	//	return err
	//}
	//
	//var values = ""
	//for k, roleID := range req.RoleIDs {
	//	values += fmt.Sprintf("(%d)", roleID)
	//	if k < len(req.RoleIDs)-1 {
	//		values += ","
	//	}
	//}
	//
	//if err = database.DB.Exec("insert into mail_" + table + "(role_id) values " + values).Error; err != nil {
	//	return err
	//}
	//
	//if err = database.DB.Exec("insert into mails" + table + "(role_id) values " + values).Error; err != nil {
	//	return err
	//}

	switch req.From {
	case pb.MailFrom_System:

		for _, roleID := range req.RoleIDs {
			models.CreateSystemMail(database.DB, roleID, req.Title, req.Content, req.Attachment)
		}

	case pb.MailFrom_GM:

		for _, roleID := range req.RoleIDs {
			models.CreateGMMail(database.DB, roleID, req.Title, req.Content, req.Attachment)
		}
	}

	return nil
}

func (h *MailServiceHandler) SystemMail(ctx context.Context, req *service.SystemMailRequest, e *empty.Empty) error {
	return nil
}

func buildPbMail(mail entity.Mail) *pb.Mail {

	return &pb.Mail{
		ID:         mail.ID,
		From:       mail.FromType,
		Title:      mail.Title,
		Content:    mail.Content,
		Read:       mail.Read,
		Timestamp:  mail.CreatedAt.Unix(),
		Attachment: mail.AttachmentStruct,
	}
}
